home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Byte.java < prev    next >
Text File  |  1998-09-22  |  7KB  |  260 lines

  1. /*
  2.  * @(#)Byte.java    1.8 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. /**
  18.  *
  19.  * The Byte class is the standard wrapper for byte values.
  20.  *
  21.  * @author  Nakul Saraiya
  22.  * @version 1.8, 07/01/98
  23.  * @see     java.lang.Number
  24.  * @since   JDK1.1
  25.  */
  26. public final
  27. class Byte extends Number {
  28.  
  29.     /**
  30.      * The minimum value a Byte can have.
  31.      *
  32.      * @since   JDK1.1
  33.      */
  34.     public static final byte   MIN_VALUE = -128;
  35.  
  36.     /**
  37.      * The maximum value a Byte can have.
  38.      *
  39.      * @since   JDK1.1
  40.      */
  41.     public static final byte   MAX_VALUE = 127;
  42.  
  43.     /**
  44.      * The Class object representing the primitive type byte.
  45.      *
  46.      * @since   JDK1.1
  47.      */
  48.     public static final Class    TYPE = Class.getPrimitiveClass("byte");
  49.  
  50.     /**
  51.      * Returns a new String object representing the specified Byte. The radix
  52.      * is assumed to be 10.
  53.      *
  54.      * @param b    the byte to be converted
  55.      * @since   JDK1.1
  56.      */
  57.     public static String toString(byte b) {
  58.     return Integer.toString((int)b, 10);
  59.     }
  60.  
  61.     /**
  62.      * Assuming the specified String represents a byte, returns
  63.      * that byte's value. Throws an exception if the String cannot
  64.      * be parsed as a byte.  The radix is assumed to be 10.
  65.      *
  66.      * @param s        the String containing the byte
  67.      * @exception    NumberFormatException If the string does not
  68.      *            contain a parsable byte.
  69.      * @since   JDK1.1
  70.      */
  71.     public static byte parseByte(String s) throws NumberFormatException {
  72.     return parseByte(s, 10);
  73.     }
  74.  
  75.     /**
  76.      * Assuming the specified String represents a byte, returns
  77.      * that byte's value. Throws an exception if the String cannot
  78.      * be parsed as a byte.
  79.      *
  80.      * @param s        the String containing the byte
  81.      * @param radix    the radix to be used
  82.      * @exception    NumberFormatException If the String does not
  83.      *            contain a parsable byte.
  84.      * @since   JDK1.1
  85.      */
  86.     public static byte parseByte(String s, int radix)
  87.     throws NumberFormatException {
  88.     int i = Integer.parseInt(s, radix);
  89.     if (i < MIN_VALUE || i > MAX_VALUE)
  90.         throw new NumberFormatException();
  91.     return (byte)i;
  92.     }
  93.  
  94.     /**
  95.      * Assuming the specified String represents a byte, returns a
  96.      * new Byte object initialized to that value.  Throws an
  97.      * exception if the String cannot be parsed as a byte.
  98.      *
  99.      * @param s        the String containing the integer
  100.      * @param radix     the radix to be used
  101.      * @exception    NumberFormatException If the String does not
  102.      *            contain a parsable byte.
  103.      * @since   JDK1.1
  104.      */
  105.     public static Byte valueOf(String s, int radix)
  106.     throws NumberFormatException {
  107.     return new Byte(parseByte(s, radix));
  108.     }
  109.  
  110.     /**
  111.      * Assuming the specified String represents a byte, returns a
  112.      * new Byte object initialized to that value.  Throws an
  113.      * exception if the String cannot be parsed as a byte.
  114.      * The radix is assumed to be 10.
  115.      *
  116.      * @param s        the String containing the integer
  117.      * @exception    NumberFormatException If the String does not
  118.      *            contain a parsable byte.
  119.      * @since   JDK1.1
  120.      */
  121.     public static Byte valueOf(String s) throws NumberFormatException {
  122.     return valueOf(s, 10);
  123.     }
  124.  
  125.     /**
  126.      * Decodes a String into a Byte.  The String may represent
  127.      * decimal, hexadecimal, and octal numbers.
  128.      *
  129.      * @param nm the string to decode
  130.      * @since   JDK1.1
  131.      */
  132.     public static Byte decode(String nm) throws NumberFormatException {
  133.     if (nm.startsWith("0x")) {
  134.         return Byte.valueOf(nm.substring(2), 16);
  135.     }
  136.     if (nm.startsWith("#")) {
  137.         return Byte.valueOf(nm.substring(1), 16);
  138.     }
  139.     if (nm.startsWith("0") && nm.length() > 1) {
  140.         return Byte.valueOf(nm.substring(1), 8);
  141.     }
  142.  
  143.     return Byte.valueOf(nm);
  144.     }
  145.  
  146.     /**
  147.      * The value of the Byte.
  148.      */
  149.     private byte value;
  150.  
  151.     /**
  152.      * Constructs a Byte object initialized to the specified byte value.
  153.      *
  154.      * @param value    the initial value of the Byte
  155.      * @since   JDK1.1
  156.      */
  157.     public Byte(byte value) {
  158.     this.value = value;
  159.     }
  160.  
  161.     /**
  162.      * Constructs a Byte object initialized to the value specified by the
  163.      * String parameter.  The radix is assumed to be 10.
  164.      *
  165.      * @param s        the String to be converted to a Byte
  166.      * @exception    NumberFormatException If the String does not
  167.      *            contain a parsable byte.
  168.      * @since   JDK1.1
  169.      */
  170.     public Byte(String s) throws NumberFormatException {
  171.     this.value = parseByte(s);
  172.     }
  173.  
  174.     /**
  175.      * Returns the value of this Byte as a byte.
  176.      *
  177.      * @since   JDK1.1
  178.      */
  179.     public byte byteValue() {
  180.     return value;
  181.     }
  182.  
  183.     /**
  184.      * Returns the value of this Byte as a short.
  185.      *
  186.      * @since   JDK1.1
  187.      */
  188.     public short shortValue() {
  189.     return (short)value;
  190.     }
  191.  
  192.     /**
  193.      * Returns the value of this Byte as an int.
  194.      *
  195.      * @since   JDK1.1
  196.      */
  197.     public int intValue() {
  198.     return (int)value;
  199.     }
  200.  
  201.     /**
  202.      * Returns the value of this Byte as a long.
  203.      *
  204.      * @since   JDK1.1
  205.      */
  206.     public long longValue() {
  207.     return (long)value;
  208.     }
  209.  
  210.     /**
  211.      * Returns the value of this Byte as a float.
  212.      *
  213.      * @since   JDK1.1
  214.      */
  215.     public float floatValue() {
  216.     return (float)value;
  217.     }
  218.  
  219.     /**
  220.      * Returns the value of this Byte as a double.
  221.      *
  222.      * @since   JDK1.1
  223.      */
  224.     public double doubleValue() {
  225.     return (double)value;
  226.     }
  227.  
  228.     /**
  229.      * Returns a String object representing this Byte's value.
  230.      *
  231.      * @since   JDK1.1
  232.      */
  233.     public String toString() {
  234.     return String.valueOf((int)value);
  235.     }
  236.  
  237.     /**
  238.      * Returns a hashcode for this Byte.
  239.      *
  240.      * @since   JDK1.1
  241.      */
  242.     public int hashCode() {
  243.     return (int)value;
  244.     }
  245.  
  246.     /**
  247.      * Compares this object to the specified object.
  248.      *
  249.      * @param obj    the object to compare with
  250.      * @return         true if the objects are the same; false otherwise.
  251.      * @since   JDK1.1
  252.      */
  253.     public boolean equals(Object obj) {
  254.     if ((obj != null) && (obj instanceof Byte)) {
  255.         return value == ((Byte)obj).byteValue();
  256.     }
  257.     return false;
  258.     }
  259. }
  260.